//
//  MenuViewController.m
//  Tom
//
//  Created by Edenyorke on 14/12/11.
//  Copyright (c) 2011 Edenyorke. All rights reserved.
//

#import "MenuViewController.h"
#import "MessageUtils.h"
#import "DateUtils.h"
#import "HelpViewController.h"

@implementation MenuViewController

@synthesize messageUtils;
@synthesize dateUtils;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"Commun.button.settings",@"") image:[UIImage imageNamed:@"settings.png"] tag:0];
    }

     
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    //To have a background
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    UITableView *tv = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [self.view addSubview:tv];
    [self.view sendSubviewToBack:tv];
     messageUtils =  [[MessageUtils alloc] init];
     dateUtils =  [[DateUtils alloc] initWithHours];
    [self initParameters];
   
}

- (void)viewDidUnload
{
    dateDebutLabel = nil;
    dateDebutValue = nil;
    dateFinValue = nil;
    dateFinLabel = nil;
    periodeValue = nil;
    periodeLabel = nil;
    messageUtils = nil;
    dateUtils = nil;
    periodeActiveLabel = nil;
    buttonBack = nil;
    buttonHelp = nil;
    titleLabel = nil;
    actionSheetDateDebut = nil;
    datePickerDateDebut = nil;
    actionSheetDateFin = nil;
    datePickerDateFin = nil;
    actionSheetPeriode = nil;
    periodeNumberArray = nil;
    periodeTypeArray = nil;
    periodeTypeEnumArray = nil;
    periodePicker = nil;
    periodeTypeEnumString = nil;
    periodeNumberString = nil;
    [super viewDidUnload];
  
}




- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (IBAction)processRetour {
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)processAide {
    HelpViewController *helpView = [[HelpViewController alloc] init];
    helpView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:helpView animated:YES];
}


- (IBAction)setDateDebut{
    
    NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"\n\n\n\n\n\n\n\n\n" : @"\n\n\n\n\n\n\n\n\n\n\n\n" ; 
    actionSheetDateDebut = [[UIActionSheet alloc]  
                                  initWithTitle:[NSString stringWithFormat:@"%@%@", title, NSLocalizedString(@"dateDebut", @"")] 
                                  delegate:self cancelButtonTitle:NSLocalizedString(@"Commun.button.cancel", @"") destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"Commun.button.select", @""), nil]; 
    [actionSheetDateDebut showInView:self.view]; 
    datePickerDateDebut = [[UIDatePicker alloc] init]; 
    datePickerDateDebut.datePickerMode = UIDatePickerModeDateAndTime; 
    [actionSheetDateDebut addSubview:datePickerDateDebut]; 
    
    NSString *dateSettings =  [messageUtils getLabel:@"dateDebut"];
    if (dateSettings != nil && !([dateSettings isEqualToString:@""])){
        datePickerDateDebut.date = [dateUtils getDateFromString:dateSettings];
    }else{
        datePickerDateDebut.date=[NSDate date];
    }

    
    
}

- (IBAction)setDateFin {
    
    NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"\n\n\n\n\n\n\n\n\n" : @"\n\n\n\n\n\n\n\n\n\n\n\n" ; 
    actionSheetDateFin = [[UIActionSheet alloc]  
                            initWithTitle:[NSString stringWithFormat:@"%@%@", title, NSLocalizedString(@"dateFin", @"")] 
                            delegate:self cancelButtonTitle:NSLocalizedString(@"Commun.button.cancel", @"") destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"Commun.button.select", @""), nil]; 
    [actionSheetDateFin showInView:self.view]; 
    datePickerDateFin = [[UIDatePicker alloc] init]; 
    datePickerDateFin.datePickerMode = UIDatePickerModeDateAndTime; 
    [actionSheetDateFin addSubview:datePickerDateFin]; 

    NSString *dateSettings =  [messageUtils getLabel:@"dateFin"];
    if (dateSettings != nil && !([dateSettings isEqualToString:@""])){
        datePickerDateFin.date = [dateUtils getDateFromString:dateSettings];
    }else{
        datePickerDateFin.date=[NSDate date];
    }
    

}


- (IBAction)setPeriode {
    
    NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"\n\n" : @"\n\n\n\n\n\n\n\n\n\n\n\n" ; 
    actionSheetPeriode = [[UIActionSheet alloc]  
                          initWithTitle:[NSString stringWithFormat:@"%@%@", title, NSLocalizedString(@"periodicity", @"")] 
                          delegate:self cancelButtonTitle:NSLocalizedString(@"Commun.button.cancel", @"") destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"Commun.button.select", @""), nil]; 
    [actionSheetPeriode showInView:self.view]; 
    periodePicker = [[UIPickerView alloc] init];
    
    [periodePicker setDelegate:self];
    [periodePicker setDataSource:self];
    [actionSheetPeriode addSubview:periodePicker]; 

}


-(IBAction) setPeriodeActive {
   
    BOOL periodeActiveSettings = [messageUtils getBool:@"isActive"]; 
    if (!periodeActiveSettings){
        [messageUtils setValueForLabel:@"isActive" value:@"YES"];
        [periodeActiveValue setText:NSLocalizedString(@"Yes", @"")];
    
    }else{
        [messageUtils setValueForLabel:@"isActive" value:@"NO"];
        [periodeActiveValue setText:NSLocalizedString(@"No", @"")];    
    }

     
}


-(void)actionSheet:(UIActionSheet *)action clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (action == actionSheetDateDebut){
        if (buttonIndex == 0) {
            NSDate *pickerDate = [datePickerDateDebut  date];      
            NSString *selectionString = [dateUtils getStringFromDate:pickerDate];
            [messageUtils setValueForLabel:@"dateDebut" value:selectionString];
            [dateDebutValue setText:selectionString];
            NSLog(@"Date debut : %@",selectionString);
        }       
    }else if(action == actionSheetDateFin){
        if (buttonIndex == 0) {
            NSDate *pickerDate = [datePickerDateFin  date];      
            NSString *selectionString = [dateUtils getStringFromDate:pickerDate];
            [messageUtils setValueForLabel:@"dateFin" value:selectionString]; 
            [dateFinValue setText:selectionString];
        }
    }else if(action == actionSheetPeriode){
        if (buttonIndex == 0) {
            [messageUtils setValueForLabel:@"periodeType" value:periodeTypeEnumString];
            [messageUtils setValueForLabel:@"periodeNumber" value:periodeNumberString];
            [self setPeriodeValue];
        }
    }
    
}

- (void)viewWillAppear:(BOOL)animated {
    [self initParameters];
}

- (void)setPeriodeValue {
    NSString *periodeCompleteValue = [[NSString alloc]init];
    
    BOOL isOk = false;
    NSString *periodeTypeSettings = [messageUtils getLabel:@"periodeType"]; 
    if (periodeTypeSettings == nil || [periodeTypeSettings isEqualToString:@""]){
        [periodeValue setText:NSLocalizedString(@"Menu.label.non.defini", @"")];
        isOk = false;
    }else{
        isOk = true;
        if ([periodeTypeSettings isEqualToString:@"WEEK"]){
            periodeCompleteValue = NSLocalizedString(@"Menu.periode.week", @"");
        }else if([periodeTypeSettings isEqualToString:@"MONTH"]){
            periodeCompleteValue = NSLocalizedString(@"Menu.periode.month", @"");
        }else{
            periodeCompleteValue = NSLocalizedString(@"Menu.periode.time", @"");
            
        }
    }
    
    
    NSString *periodeNumberSettings = [messageUtils getLabel:@"periodeNumber"];
    if (periodeNumberSettings == nil || [periodeNumberSettings isEqualToString:@""]){
        isOk = false;
        [periodeValue setText:NSLocalizedString(@"Menu.label.non.defini", @"")];
    }else{
        isOk = true;
    }
    
    
    if (isOk){
        [periodeValue setText:[NSString stringWithFormat:@"%@%@%@", periodeCompleteValue, @" ", periodeNumberSettings]];
    }
}

- (void)initParameters {
    
    
       NSString *dateDebutSettings =  [messageUtils getLabel:@"dateDebut"];
    if (dateDebutSettings == nil || [dateDebutSettings isEqualToString:@""]){
        [dateDebutValue setText:NSLocalizedString(@"Menu.label.non.defini", @"")];
    }else{
        [dateDebutValue setText:dateDebutSettings];
    }
    
    NSString *dateFinSettings = [messageUtils getLabel:@"dateFin"]; 
    if (dateFinSettings == nil || [dateFinSettings isEqualToString:@""]){
        [dateFinValue setText:NSLocalizedString(@"Menu.label.non.defini", @"")];
    }else{
        [dateFinValue setText:dateFinSettings];
    }
   
    [self setPeriodeValue];
    
    BOOL periodeActiveSettings = [messageUtils getBool:@"isActive"]; 
    if (periodeActiveSettings){
        [periodeActiveValue setText:NSLocalizedString(@"Yes", @"")];
        
    }else{
        [periodeActiveValue setText:NSLocalizedString(@"Non", @"")];    
    }
    
    [dateDebutLabel setTitle: NSLocalizedString(@"Menu.button.date.debut", @"") forState:UIControlStateNormal];
    dateDebutLabel.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
   
    [dateFinLabel setTitle: NSLocalizedString(@"Menu.button.date.fin", @"") forState:UIControlStateNormal];
    dateFinLabel.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    [periodeLabel setTitle: NSLocalizedString(@"Menu.button.periode", @"") forState:UIControlStateNormal];
    periodeLabel.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

    [periodeActiveLabel setTitle: NSLocalizedString(@"Menu.button.active", @"") forState:UIControlStateNormal];
    periodeActiveLabel.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

    buttonHelp.title = NSLocalizedString(@"Commun.button.help", @"");
    buttonBack.title = NSLocalizedString(@"Commun.button.back", @"");
    titleLabel.text =  NSLocalizedString(@"Menu.title", @"");
    
    periodeTypeArray = [[NSArray alloc] initWithObjects:
                        NSLocalizedString(@"Menu.periode.week", @""), NSLocalizedString(@"Menu.periode.month", @""),NSLocalizedString(@"Menu.periode.time", @"")
                        ,nil];
    periodeTypeEnumArray = [[NSArray alloc] initWithObjects:
                            @"WEEK", @"MONTH", @"TIME"
                            ,nil];
    periodeNumberArray = [[NSArray alloc] initWithObjects:
                          @"2", @"3",@"4", nil];
    
    periodeNumberString = @"2";
    periodeTypeEnumString = @"WEEK";
     
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
    
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    
    if (component == 0) {
        return [periodeTypeArray count];
    }
    return [periodeNumberArray count];
    
}

- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component
{
    if (component == 0) {
        return [periodeTypeArray objectAtIndex:row];
    }
    return [periodeNumberArray objectAtIndex:row];
} 

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    switch(component) {
        case  0: return 200;
        case  1: return 44;
            
        default: return 22;
    }
    
    return 22;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component
{
    if (component == 0)
    {
        periodeTypeEnumString = [[NSString alloc] initWithFormat:
                                 @"%@",
                                 [periodeTypeEnumArray objectAtIndex:row]];
        NSLog(@"%@",periodeTypeEnumString);
        
    } else {
        periodeNumberString = [[NSString alloc] initWithFormat:
                               @"%@",
                               [periodeNumberArray objectAtIndex:row]];
        NSLog(@"%@",periodeNumberString);     
    }
}



@end
